package com.hui.controller.user;

import com.hui.model.car.CartIdsParam;
import com.hui.model.car.CartNumParam;
import com.hui.model.car.CartParam;
import com.hui.service.StoreCartService;
import com.hui.util.ResponseResult;
import com.hui.util.module.GetUserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 购物车控制器
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/api/cart")
public class StoreCartController {

    private final StoreCartService storeCartService;

    /**
     * 购物车 添加
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody CartParam cartParam, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        Long res = storeCartService.addCart(uid, cartParam.getProductId(), cartParam.getCartNum(),
                cartParam.getUniqueId(), cartParam.getIsNew(), cartParam.getCombinationId(),
                cartParam.getSecKillId(), cartParam.getBargainId());
        Map map = new HashMap();
        map.put("cartId", res);
        return ResponseResult.okResult(map);
    }

    /**
     * 获取购物车列表
     */
    @GetMapping("/list")
    public ResponseResult getList(HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);

        return storeCartService.getUserProductCartList(uid, null, null);
    }

    /**
     * 修改产品数量
     */
    @PostMapping("/num")
    public ResponseResult cartNum(@RequestBody CartNumParam param, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        return storeCartService.changeUserCartNum(param.getId(), param.getNumber(), uid);
    }

    /**
     * 购物车删除产品
     */
//    @NoRepeatSubmit TODO 防止重复提交
    @PostMapping("/del")
    @ApiOperation(value = "购物车删除产品", notes = "购物车删除产品")
    public ResponseResult<Boolean> cartDel(@Validated @RequestBody CartIdsParam parm, HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        storeCartService.removeUserCart(uid, parm.getIds());
        return ResponseResult.okResult();
    }

}
